var config = require('./config');
App({
  onLaunch() {
 
  },
  onShow(){
  },
  onHide()  {
  },
  globalData: {
    hasLogin: false,
    openid: '',
    userinfo: null,
    tagName: config.tagName,
    userName:'' ,
    lx_userName:''
  },
  // lazy loading openid
  getUserInfo (cb)  {
    var self = this;
    if (self.globalData.userinfo) {
      typeof cb =="function"&&cb(this.globalData.userinof)
    } else {
      wx.login({
        success: (data) =>  {
          wx.getUserInfo({
            success(res){
                self.globalData.userinfo=res.userInfo
                typeof cb == "function" && cb(self.globalData.userinof)
            }, fail: (err) =>  {
              wx.showModal({
                title: '警告',
                content: '您点击了拒绝授权，将无法正常使用******的功能体验。请10分钟后再次点击授权，或者删除小程序重新进入。',
                success:(res) => {
                  if (res.confirm) {
                  }
                }
              });
            }
        });  
        }
      });
    }
  },
  getNSessionId(data, cb) {//根据login后获取sessionId
    var that = this;
    wx.request({
      url: config.host_root_https + '/login/mini-program/' + config.tagName,
      data: { code: data.code },
      success(res) {
        var regex = /^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/;
        var reg = new RegExp(regex);
        if (reg.test(res.data)) {//使用正则表达式验证，若是通过则继续获取数据
          wx.setStorageSync(config.SESSION_ID, res.data);//存储sessionId
          that.mini_getNUsername(res.data, cb);
        }
      }
    });
  },
  getSessionId(data, cb){//根据login后获取sessionId
    var that=this;
    wx.request({
      url: config.host_root_https + '/login/mini-program/'+config.tagName,
      data: { code: data.code },
      success(res) {
        var regex = /^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/;
        var reg = new RegExp(regex);
        if (reg.test(res.data)) {//使用正则表达式验证，若是通过则继续获取数据
          wx.setStorageSync(config.SESSION_ID, res.data);//存储sessionId
          that.mini_getUsername(res.data, cb);
        }
      }
    });
  }, mini_getNUsername(sessionId, cb) {//最后一步获取username
    var that = this;
    wx.request({
      url: config.host_https + '/mini-program/username',
      data: { sessionId: sessionId },
      success(res) {
        if (res.data) {
          typeof cb == "function" && cb(res.data);
        }
        else {
          typeof cb == "function" && cb(null);
        }
      }
    });
  },
  mini_getUsername(sessionId, cb){//最后一步获取username
    var that=this;
    wx.request({
      url: config.host_https + '/mini-program/username',
      data: { sessionId: sessionId },
      success(res) {
        console.log('mini_getUsername:' + res.data);
          if (res.data) {
            typeof cb == "function" && cb(res.data);
          } 
          else {
           wx.removeStorageSync(config.SESSION_ID);
           typeof cb == "function" && cb(null);
          //  wx.login({
          //    success: function (data) {
          //      that.getSessionId(data, cb);
          //    }
          //  });   
         }
      }
    });  
  },getUserName(cb){//第一次使用,且若是为null再次获取
    var that = this; 
    wx.checkSession({
      success: function () {
        //session 未过期，并且在本生命周期一直有效
        var SESSION_ID = wx.getStorageSync(config.SESSION_ID);
        if (SESSION_ID == null || undefined == SESSION_ID || SESSION_ID == "") {
          wx.login({
            success: function (data) {
              that.getSessionId(data, cb);
            }
          })          
        }else{
          that.mini_getUsername(SESSION_ID, cb);        
        }
      },
      fail: function () {//表示session过期,那么需要从login到获取用户(一般不会调用，网络故障会出现)
        wx.login({
          success: function (data) {
            that.getSessionId(data,cb);
          }
        });
      }
    });
  },getNUserName(cb){//第二次或第n次获取username使用
    var that = this;
    wx.checkSession({
      success: function () {
        //session 未过期，并且在本生命周期一直有效
        var SESSION_ID = wx.getStorageSync(config.SESSION_ID);
        if (SESSION_ID == null || undefined == SESSION_ID || SESSION_ID == "") {
          wx.login({
            success: function (data) {
              that.getNSessionId(data, cb);
            }
          })
        } else {
          that.mini_getNUsername(SESSION_ID, cb);
        }
      },
      fail: function () {//表示session过期,那么需要从login到获取用户(一般不会调用，网络故障会出现)
        wx.login({
          success: function (data) {
            that.getSessionId(data, cb);
          }
        });
      }
    });

  },getLxUserName(cb){
    var that = this;
    var SESSION_ID = wx.getStorageSync(config.SESSION_ID);
    if (SESSION_ID == null || undefined == SESSION_ID || SESSION_ID == "") {
      wx.login({
        success: (data) => {
          wx.request({
            url: config.host_root_https + '/login/mini-program/'+ config.tagName,
            data: {
              code: data.code
            },
            success(res) {
              //将获取到的SESSION_ID存放到域中
              wx.setStorageSync(config.SESSION_ID, res.data);
              //请求处理
              wx.request({
                url: config.host_https + '/mini-program/lingxiuti-username',
                data: { sessionId: res.data },
                success(res) {
                  that.globalData.lx_userName = res.data;
                  typeof cb == "function" && cb(res.data);
                }
              });
            }
          })
        }
      });
    }else{
      wx.request({
        url: config.host_https + '/mini-program/lingxiuti-username',
        data: { sessionId: res.data },
        success(res) {
          that.globalData.lx_userName = res.data;
          typeof cb == "function" && cb(res.data);
        }
      });
    }      
  }
})
